package saxion.speelveld.spaceengineer.view.tabs;

import java.util.Observable;
import java.util.Observer;

import saxion.speelveld.spaceengineer.R;
import saxion.speelveld.spaceengineer.controller.ResourceSimpleAdapter;
import saxion.speelveld.spaceengineer.model.Game;
import saxion.speelveld.spaceengineer.model.Resource;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ListView;

public class GameResources extends LinearLayout implements Observer
{

	private Game game;

	private Resource[] resources;

	ResourceSimpleAdapter adapter;

	public GameResources(Context context)
	{
		super(context);
		this.Initialize(context);

	}

	public GameResources(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		this.Initialize(context);

	}

	public void fillList()
	{
		/**
		 * Resource type Amount
		 */
		Resource[] resources = this.game.getViewableResourceArray();
		ListView currentResources = (ListView) this.findViewById(R.id.currentResourcesList);
		adapter = new ResourceSimpleAdapter(this.getContext(), game);
		currentResources.setAdapter(adapter);
	}

	/**
	 * Ini voor all Check op game object not null
	 * 
	 * @param context
	 */
	public void Initialize(Context context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab_game_resources, this);
	}

	public void setGameModel(Game game)
	{
		this.game = game;
		game.addObserver(this);
		this.fillList();
	}

	/**
	 * Check op game object not null
	 * 
	 * @return boolean
	 */
	private boolean checkIfGameIsNotNull()
	{
		if (this.game != null)
		{
			return true;
		}

		return false;
	}

	@Override
	public void update(Observable arg0, Object arg1)
	{
		if (adapter != null)
		{
			adapter.notifyDataSetChanged();
		}

	}
}
